﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetroFramework;
using NPOI.HPSF;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using BorderStyle = System.Windows.Forms.BorderStyle;
using HorizontalAlignment = System.Windows.Forms.HorizontalAlignment;

namespace Coffee_Restaurant.Models
{
    class Export
    {
        static HSSFWorkbook hssfworkbook;

        public static void WriteToStream(string filename, string folder)
        {

            try
            {
                //create folder if not exist
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }

                //Write the stream data of workbook to the root directory
                using (var file = new FileStream(filename, FileMode.OpenOrCreate))
                {
                    hssfworkbook.Write(file);
                    file.Close();

                }
                if (File.Exists(filename))
                {
                    CustomMessageBox.Show("Đã ghi thành công file " + filename, "", "alert", MetroColorStyle.Green);
                }
            }
            catch (Exception)
            {
                CustomMessageBox.Show("Lỗi ghi file", "", "alert", MetroColorStyle.Green);
            }
        }

        public static void GenerateData(string title, DataGridView gridView)
        {
            ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
            DataGridViewColumnCollection column = gridView.Columns;
            //merger celll row 0
            sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 0, (column.Count - 1)));
            //create heading 0
            IRow rowHeadingTitle = sheet1.CreateRow(0);

            ICellStyle style1 = hssfworkbook.CreateCellStyle();
            style1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER; //align
            //font
            IFont font1 = hssfworkbook.CreateFont();
            font1.Color = HSSFColor.BLACK.index;
            font1.Boldweight = (short)FontBoldWeight.BOLD;
            font1.FontHeight = 110 * 2; //100 font size = 5
            style1.SetFont(font1);
            rowHeadingTitle.CreateCell(0).SetCellValue(title);
            rowHeadingTitle.Cells[0].CellStyle = style1;

            //create heading 1
            IRow rowHeading = sheet1.CreateRow(1);
            for (int j = 0; j < column.Count; j++)
            {
                sheet1.SetColumnWidth(j, 200 * 37); // 37 = 1px => 200px

                ICellStyle style = hssfworkbook.CreateCellStyle();
                style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER; //align
                //border
                style.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
                style.BottomBorderColor = HSSFColor.ORANGE.index;
                style.BorderLeft = NPOI.SS.UserModel.BorderStyle.DASH_DOT_DOT;
                style.LeftBorderColor = HSSFColor.GREEN.index;
                style.BorderRight = NPOI.SS.UserModel.BorderStyle.HAIR;
                style.RightBorderColor = HSSFColor.BLUE.index;
                style.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
                style.TopBorderColor = HSSFColor.ORANGE.index;
                //font
                IFont font = hssfworkbook.CreateFont();
                font.Color = HSSFColor.ORANGE.index;
                font.Boldweight = (short)FontBoldWeight.BOLD;
                //font.FontHeight = 100 * 2; //100 font size = 5
                style.SetFont(font);
                rowHeading.CreateCell(j).SetCellValue(column[j].HeaderText);
                rowHeading.Cells[j].CellStyle = style;
            }
            //create body
            for (int i = 0; i < gridView.RowCount; i++)
            {
                //tang 2 vi tri vi 0 la heading
                IRow row = sheet1.CreateRow(i + 2);
                for (int j = 0; j < column.Count; j++)
                {
                    ICellStyle style = hssfworkbook.CreateCellStyle();
                    style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;

                    row.CreateCell(j).SetCellValue(gridView.Rows[i].Cells[j].Value.ToString());
                    row.Cells[j].CellStyle = style;
                }
            }
        }

        public static void InitializeWorkbook()
        {
            hssfworkbook = new HSSFWorkbook();

            ////create a entry of DocumentSummaryInformation
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "0968868862";
            hssfworkbook.DocumentSummaryInformation = dsi;

            ////create a entry of SummaryInformation
            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "Báo cáo";
            hssfworkbook.SummaryInformation = si;
        }
    }
}
